package org.hackystat.projectbrowser.page.chooseTAP;

import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;
import org.hackystat.projectbrowser.ProjectBrowserSession;

/**
 * The class that will edit the Trigger/Action pair name
 * 
 * @author Edward Kim
 * 
 */
public class EditTAPTable extends StatelessForm {

  /** Supports serialization. */
  private static final long serialVersionUID = 1L;

  private String oldTAPid;
  private String tempTAP;
  private String projectName;
  private PageParameters responseParam = new PageParameters();

  /**
   * The form that will edit the Trigger/Action pair name
   * 
   * @param id param for the super class
   * @param tapId The current tap name
   * @param projectName The current project
   */
  public EditTAPTable(String id, String tapId, String projectName) {
    super(id);
    this.oldTAPid = tapId;
    this.projectName = projectName;
    responseParam.add("projectName", this.projectName);

    setModel(new CompoundPropertyModel(this));
    add(new TextField("tempTAP"));
  }

  /**
   * When the form is submiting, check for errors and try to create a new project.
   */
  @Override
  public void onSubmit() {
    Project project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(this.projectName);
    List<TriggerActionPair> pairs = project.getTriggerActionPairs().getTriggerActionPair();
    for (TriggerActionPair pair : pairs) {
      if (pair.getId().equals(this.oldTAPid)) {
        pair.setId(tempTAP);
      }
    }

    // set response page to go back to chooseTAP
    setResponsePage(ChooseTAP.class, responseParam);
  }

  /**
   * setter for tapId.
   * 
   * @param tapId The new tap id.
   */
  public void setTAPid(String tapId) {
    this.oldTAPid = tapId;
  }

  /**
   * getter for TAPid.
   * 
   * @return returns the TAP ID.
   */
  public String getTAPid() {
    return oldTAPid;
  }

  /**
   * setter for tempTAP.
   * 
   * @param tempTAP The new TAP name.
   */
  public void setTempTAP(String tempTAP) {
    this.tempTAP = tempTAP;
  }

  /**
   * getter for tempTAP.
   * 
   * @return returns the tempTAP.
   */
  public String getTempTAP() {
    return tempTAP;
  }

}
